package cn.framelife.rainbow.ui.dialog;

import java.util.List;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;

import cn.framelife.rainbow.util.FileUtil;
import cn.framelife.rainbow.util.StringUtil;

/**
 * Java Source Folder Dialog
 * 
 * @ClassName: JavaSourceFolderDialog 
 * @author 潘广伟  p_3er@qq.com 
 * @date 2013年11月21日 下午3:41:15 
 *
 */
public class JavaPackageDialog extends Dialog {
	private Tree packageTree;
	private String selectionName;
	private String projectName;
	
	
	public String getSelectionName() {
		return selectionName;
	}

	/**
	 * Create the dialog.
	 * @param parentShell
	 */
	public JavaPackageDialog(Shell parentShell,String projectName) {
		super(parentShell);
		this.projectName = projectName;
	}
	
	

	/**
	 * Create contents of the dialog.
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		this.getShell().setText("Java Package");
		
		Composite container = (Composite) super.createDialogArea(parent);
		
		packageTree = new Tree(container, SWT.SINGLE);
		packageTree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		List<String> itemNames = FileUtil.getChildPackage(projectName, "/src");
		
		for(String itemName : itemNames){
			TreeItem item = new TreeItem(packageTree, SWT.NONE);
			item.setText(itemName);
		}	
		
		packageTree.addMouseListener(new MouseAdapter() {
			public void mouseDoubleClick(MouseEvent e) {
			
			}

			@Override
			public void mouseDown(MouseEvent e) {
				TreeItem selected = packageTree.getItem(new Point(e.x, e.y)); // 取节点控件
				if(selected.getItemCount() == 0){
					List<String> itemNames = FileUtil.getChildPackage(projectName,selected.getText());
					for(String itemName : itemNames){
						TreeItem item = new TreeItem(selected, SWT.NONE);
						item.setText(StringUtil.merge(selected.getText(),".",itemName));
					}	
					selected.setExpanded(true);
				}
			}		
		});

		return container;
	}
	
    /**
     * dialog button pressed
     */
	protected void buttonPressed(int buttonId) {
		if (buttonId == IDialogConstants.OK_ID && packageTree.getSelection().length > 0) {
			this.selectionName = packageTree.getSelection()[0].getText();
		}
		super.buttonPressed(buttonId);
	}

	/**
	 * Create contents of the button bar.
	 * @param parent
	 */
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL,
				true);
		createButton(parent, IDialogConstants.CANCEL_ID,
				IDialogConstants.CANCEL_LABEL, false);
	}

	/**
	 * Return the initial size of the dialog.
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(450, 300);
	}

}
